package com.zzh.myapplication.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.R;
import net.icarplus.car.activity.BaoMainCarActivity;
import net.icarplus.car.activity.CouponActiviity;
import net.icarplus.car.activity.DayRentCarActivity;
import net.icarplus.car.activity.DivideTimeCarActivity;
import net.icarplus.car.activity.LoginActivity;
import net.icarplus.car.activity.RoadtripActivity;
import net.icarplus.car.activity.SelectCityActivity;
import net.icarplus.car.activity.VehicleControlActivity;
import net.icarplus.car.anim.CubeTransformer;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseFragment;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/3 11:16
 * 修改人：Gophy
 * 修改时间：16/3/3 11:16
 * 修改备注：
 */
public class MainFragment extends BaseFragment implements ViewPager.OnPageChangeListener {

    private ViewPager vpBanner;
    private ViewGroup group;
    private RelativeLayout rl_select_city;
    private RelativeLayout rl_call_num;
    private RelativeLayout rl_bao_che;
    private RelativeLayout rl_use_car_now;
    private RelativeLayout rl_divide_rent_car;
    private RelativeLayout rl_day_rent_car;
    private RelativeLayout rl_travel;
    private RelativeLayout rl_short_rent;
    private TextView curCity;
    private int[] pointId = {R.id.iv1, R.id.iv2, R.id.iv3};
    private SimpleDraweeView iv[] = new SimpleDraweeView[3];
    private ImageView points[] = new ImageView[3];
    private boolean isFrist = false;
    private int index;
    private LoadingUtil loadingUtil;
    private JSONObject jsons;
    private JSONArray array;
    private RelativeLayout rl_coupon_content;

    @Override
    public void getsavedInstanceState(Bundle savedInstanceState) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_main, null);
    }

    @Override
    public void initComponent(View view) {
        rl_select_city = (RelativeLayout) view.findViewById(R.id.rl_select_city);
        rl_coupon_content = (RelativeLayout) view.findViewById(R.id.rl_coupon_content);
        rl_call_num = (RelativeLayout) view.findViewById(R.id.rl_call_num);
        rl_use_car_now = (RelativeLayout) view.findViewById(R.id.rl_use_car_now);
        rl_divide_rent_car = (RelativeLayout) view.findViewById(R.id.rl_divide_rent_car);
        rl_day_rent_car = (RelativeLayout) view.findViewById(R.id.rl_day_rent_car);
        rl_bao_che = (RelativeLayout) view.findViewById(R.id.rl_bao_che);
        curCity = (TextView) view.findViewById(R.id.tv_city);
        rl_travel = (RelativeLayout) view.findViewById(R.id.rl_travel);
        vpBanner = (ViewPager) view.findViewById(R.id.vp_banner);
        vpBanner.setPageTransformer(true, new CubeTransformer());
        vpBanner.setAdapter(adapter1);
        vpBanner.setCurrentItem(0);
        for (int i = 0; i < 3; i++) {
            iv[i] = new SimpleDraweeView(getActivity());
            iv[i].setScaleType(ImageView.ScaleType.FIT_XY);
            points[i] = (ImageView) view.findViewById(pointId[i]);
        }
        if (!isFrist && pointId.length > 1) {
            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler1.sendEmptyMessage(0);
                    }
                }
            }.start();
            points[0].setSelected(true);
            isFrist = true;
        }
        this.loadingUtil = new LoadingUtil(getActivity(), "查询中...");
    }

    public void setCarListsParams() {
        JSONObject json = new JSONObject();
        try {
            json.put("imgType", "0101");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.loadingUtil.isShowLoading();
        OkHttpUtils.get().url(UrlValues.BANNER_PIC).addParams("input", json.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                getBannerPics(response);
            }
        });
    }

    public void getBannerPics(String data) {
        try {
            JSONObject result = new JSONObject(data.toString());
            array = result.getJSONArray("queryList");
            for (int i = 0; i < array.length(); i++) {
                jsons = array.getJSONObject(i);
                iv[i].setImageURI(Uri.parse(UrlValues.CAR_IMG_ID + jsons.getString("imgUrl")));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    Handler handler1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            vpBanner.setCurrentItem((vpBanner.getCurrentItem() + 1) % 3);
        }

        ;
    };
    PagerAdapter adapter1 = new PagerAdapter() {
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        public int getCount() {
            return iv.length;
        }

        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(iv[position % iv.length]);
        }

        ;

        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(iv[position % pointId.length]);
            return iv[position % pointId.length];
        }
    };

    public void setListener() {
        rl_select_city.setOnClickListener(this);
        rl_bao_che.setOnClickListener(this);
        rl_call_num.setOnClickListener(this);
        rl_use_car_now.setOnClickListener(this);
        rl_day_rent_car.setOnClickListener(this);
        rl_divide_rent_car.setOnClickListener(this);
        rl_travel.setOnClickListener(this);
        rl_coupon_content.setOnClickListener(this);
        vpBanner.setOnPageChangeListener(this);
    }

    @Override
    public void onAfterInit() {

        setCarListsParams();

    }

    @Override
    public void onClick(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.rl_select_city:
                intent.setClass(getActivity(), SelectCityActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.rl_call_num:
                Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + getString(R.string.service_num)));
                startActivity(callIntent);
                break;
            case R.id.rl_use_car_now:
                intent.setClass(getActivity(), VehicleControlActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_divide_rent_car:
                intent.setClass(getActivity(), DivideTimeCarActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_day_rent_car:
                intent.setClass(getActivity(), DayRentCarActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_travel:
                if (!SPUtils.getBoolean(getActivity(), "isLogin", false)) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    start_Activity(intent);
                    T.show(getActivity(),"请先登录",1);
                } else {
                    intent.setClass(getActivity(), RoadtripActivity.class);
                    start_Activity(intent);
                }
                break;
            case R.id.rl_bao_che:
                intent.setClass(getActivity(), BaoMainCarActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_coupon_content:
                intent.setClass(getActivity(), CouponActiviity.class);
                start_Activity(intent);

                break;
            default:
                break;
        }
    }

    public void setCurrentCity(String city) {
        if (city == null) {
            return;
        }
        curCity.setText(city);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        points[index].setSelected(false);
        points[position].setSelected(true);
        index = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
